package com.minjiang.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minjiang.common.result.Result;
import com.minjiang.model.system.MyComment;
import com.minjiang.model.system.MyDynamic;
import com.minjiang.model.system.MyUser;
import com.minjiang.model.vo.MyDynamicQueryVo;
import com.minjiang.model.vo.MyDynamicVo;
import com.minjiang.system.service.sec.MyCommentService;
import com.minjiang.system.service.sec.MyDynamicService;
import com.minjiang.system.service.sec.MyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "动态管理")
@RestController
@RequestMapping("/admin/system/myDynamic")
public class MyDynamicController {

    @Autowired
    private MyDynamicService myDynamicService;
    @Autowired
    private MyUserService myUserService;
    @Autowired
    private MyCommentService myCommentService;

    @ApiOperation(value = "获取动态分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
                    MyDynamicQueryVo dynamicQueryVo) {
        Page<MyDynamic> pageParam = new Page<>(page, limit);
        IPage<MyDynamic> pageModel = myDynamicService.selectPage(pageParam, dynamicQueryVo);
        List<MyDynamic> records = pageModel.getRecords();
        List<MyDynamicVo> myDynamicVos=new ArrayList<>();
        for (MyDynamic record : records) {
            MyDynamicVo myDynamicVo = new MyDynamicVo();
            BeanUtils.copyProperties(record,myDynamicVo);
            MyUser myUser = myUserService.getById(record.getUserId());
            myDynamicVo.setUserName(myUser.getNickname());
            myDynamicVos.add(myDynamicVo);
        }
        Page<MyDynamicVo> myDynamicVoIPage = new Page<MyDynamicVo>();
        myDynamicVoIPage.setTotal(pageModel.getTotal());
        myDynamicVoIPage.setCurrent(pageModel.getCurrent());
        myDynamicVoIPage.setRecords(myDynamicVos);
        myDynamicVoIPage.setSize(pageModel.getSize());
        myDynamicVoIPage.setPages(pageModel.getPages());

        return Result.ok(myDynamicVoIPage);
    }

    @ApiOperation(value = "根据id获取动态")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Integer id) {
        MyDynamic dynamic = myDynamicService.getById(id);
        MyUser myUser = myUserService.getById(dynamic.getUserId());
        MyDynamicVo myDynamicVo = new MyDynamicVo();
        BeanUtils.copyProperties(dynamic,myDynamicVo);
        myDynamicVo.setUserName(myUser.getNickname());
        return Result.ok(myDynamicVo);
    }

    @ApiOperation(value = "删除动态")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id){
        LambdaQueryWrapper<MyComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MyComment::getMid,id);
//        先删除评论
        boolean remove = myCommentService.remove(queryWrapper);
//            在删除动态
            boolean isOk = myDynamicService.removeById(id);
            if (isOk) {
                return Result.ok();
            }

        return Result.fail("删除动态失败");
    }


    @ApiOperation(value = "更新动态")
    @PutMapping("/update")
    public Result updateById(@RequestBody MyDynamic dynamic) {
        myDynamicService.updateById(dynamic);
        return Result.ok();
    }
}
